package com.my.main.base.service.Impl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.BaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service基类的实现
 *
 * @author Liuhx	2020-01-16
 */
@SuppressWarnings("serial")
@Transactional
public class BaseServiceImpl<T> implements BaseService<T>, Serializable {

	/** 日志 */
	protected Log logger = LogFactory.getLog(getClass());

	private BaseMapper<T> baseMapper;
	@Resource
	public void setBaseMapper(BaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
	}


	@Override
	public int isSave(T object) {

		return baseMapper.isSave(object);
	}
	@Override
	public void isDeleteByIntId(Integer id) {
		baseMapper.isDeleteByIntId(id);
	}

	@Override
	public void isDeleteLongId(Long id) {
		baseMapper.isDeleteLongId(id);
	}

	@Override
	public void isDeleteByUuid(String uuid) {
		baseMapper.isDeleteByUuid(uuid);
	}

	@Override
	public void isDeleteByObject(Object object) {
		baseMapper.isDeleteByObject(object);
	}

	@Override
	public int isDeleteByIds(Map<String,Object> map) {
		List idList = new ArrayList();
		String ids = "";
        if (!StringUtils.isEmpty(map.get("ids"))) {
			ids = map.get("ids").toString();
            String[] strs = ids.split(",");
            for (String str : strs){
                idList.add(str);
            }
		}
		HashMap<String, List<String>> idsMap = new HashMap<>();
		idsMap.put("id",idList );
		return baseMapper.isDeleteByIds(idsMap);
	}

	@Override
	public int isDeleteById(Map<String,Object> map) {
		Long id = 0l;
		if (!StringUtils.isEmpty(map.get("id"))) {
			id = Long.parseLong(map.get("id").toString());
		}
		return baseMapper.isDeleteLongId(id);
	}
	@Override
	public void isUpdate(T object) {
		baseMapper.isUpdate(object);
	}

	@Override
	public T findByIntId(Integer obj_id) {
		return baseMapper.findByIntId(obj_id);
	}

	@Override
	public T findByLongId(Long obj_id) {
		return baseMapper.findByLongId(obj_id);
	}

	@Override
	public T findByUuid(String uuid) {
		return baseMapper.findByUuid(uuid);
	}

	@Override
	public T findById(Map<String,Object> map) {
		Long id = 0l;
		if (!StringUtils.isEmpty(map.get("id"))) {
			id = Long.parseLong(map.get("id").toString());
		}
		return baseMapper.findByLongId(id);
	}

	@Override
	public T findByObject(Object object) {
		return baseMapper.findByObject(object);
	}

	@Override
	public long findCount() {
		return baseMapper.findCount();
	}

	@Override
	public long findMax(String maxColumn) {
		return baseMapper.findMax(maxColumn);
	}

	@Override
	public long findSum(String sumColumn) {
		return baseMapper.findSum(sumColumn);
	}

	@Override
	public T queryByIntId(Integer obj_id) {
		return baseMapper.queryByIntId(obj_id);
	}

	@Override
	public T queryByLongId(Long obj_id) {
		return baseMapper.queryByLongId(obj_id);
	}

	@Override
	public T queryByUuid(String uuid) {
		return baseMapper.findByUuid(uuid);
	}

	@Override
	public T queryStringId(String id) {
		return baseMapper.queryByStringId(id);
	}

	@Override
	public T queryByObject(Object object) {
		return baseMapper.queryByObject(object);
	}

	@Override
	public List<T> queryList(Map<String, Object> map) {
		return baseMapper.queryListByMap(map);
	}

	@Override
	public List<T> queryList(int start, int end) {
		return baseMapper.queryList(start,end);
	}

	@Override
	public long queryCount() {
		return baseMapper.queryCount();
	}

	@Override
	public long queryCount(Map<String, Object> map) {
		return baseMapper.queryCountByMap(map);
	}

	@Override
	public long queryMax(String maxColumn) {
		return baseMapper.queryMax(maxColumn);
	}

	@Override
	public long querySum(String sumColumn) {
		return baseMapper.querySum(sumColumn);
	}
}
